package cn.com.pajx.pajx_visitor_v3.api;

import java.util.concurrent.TimeUnit;

import okhttp3.OkHttpClient;
import okhttp3.logging.HttpLoggingInterceptor;
import retrofit2.Retrofit;
import retrofit2.adapter.rxjava2.RxJava2CallAdapterFactory;
import retrofit2.converter.gson.GsonConverterFactory;
import retrofit2.converter.scalars.ScalarsConverterFactory;

/**
 * create by ruancw on 2024/8/26
 * Description:
 */
public class DownloadApiService {

    private static ApiService downloadApiService;

    private static final Retrofit retrofit = new Retrofit.Builder().baseUrl(Api.BASE_URL)
            .addConverterFactory(ScalarsConverterFactory.create())//解决上传图片带参数添加""的问题（必须放在Gson前）
            .addConverterFactory(GsonConverterFactory.create())//将返回的数据转为Gson
            .addCallAdapterFactory(RxJava2CallAdapterFactory.create())//添加Rxjava的回调
            .client(genericClient())
            .build();


    public static ApiService getInstance() {
        if (downloadApiService == null) {
            downloadApiService = getDownloadApiService();
        }
        return downloadApiService;
    }

    private static <T> T getService(Class<T> cls) {
        return retrofit.create(cls);
    }

    private static ApiService getDownloadApiService() {
        return getService(ApiService.class);
    }

    private static OkHttpClient genericClient() {

        return new OkHttpClient.Builder()
                .connectTimeout(10, TimeUnit.SECONDS)
                .writeTimeout(5, TimeUnit.MINUTES)
                .readTimeout(5, TimeUnit.MINUTES)
                .addInterceptor(new HttpLoggingInterceptor()
                        .setLevel(HttpLoggingInterceptor.Level.BODY))
                .build();
    }


}
